package com.jdrx.sw.sewage.common.aop;

import com.alibaba.fastjson.JSONObject;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.sw.sewage.beans.vo.DeviceIotVO;
import com.jdrx.sw.sewage.common.wrapper.HeaderMapRequestWrapper;
import com.jdrx.sw.sewage.dao.DeviceDAO;
import com.jdrx.sw.sewage.service.common.RedisService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

import static com.jdrx.sw.sewage.common.constant.Constant.MQ_DEVICE_KEY;

@Aspect
@Component
public class MQDeviceRefreshAop {
    public static final Logger logger = LoggerFactory.getLogger(MQDeviceRefreshAop.class);

    @Autowired
    private DeviceDAO deviceDAO;
    @Autowired
    private RedisService redisService;

    @Pointcut("@annotation(com.jdrx.sw.sewage.beans.anno.MQDeviceCacheRefresh)")
    public void refreshMQDeviceCache() {
    }

    @Before("refreshMQDeviceCache()")
    public void Before(JoinPoint joinPoint) throws BizException, IOException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        if (request.getRequestURI().contains("api/devicestorage/scrap")) {
            logger.info("aop执行,开始刷新mq设备缓存");
            Long devId = Long.valueOf(request.getParameter("devId"));
            List<String> points = deviceDAO.findDevPointBydevId(devId);
            if (null != points && points.size() <= 0) {
                logger.info("设备集合为空,mq设备缓存不删除");
                return;
            }
            //如过keys删除，谨慎！！！！
            points.forEach(item -> {
                redisService.del(item);
            });
            logger.info("aop结束,mq设备缓存移除完成");
        }
    }

    @After("refreshMQDeviceCache()")
    public void after(JoinPoint joinPoint) throws BizException, IOException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getRequestURI().contains("api/assets/assetsCollect")) {
            logger.info("aop执行,开始刷新mq设备缓存");
            HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(request);
            Long gatherDevId = JSONObject.parseObject(requestWrapper.getBody()).getLong("gatherDevId");
            List<DeviceIotVO> deviceIotVOs = deviceDAO.selectMQDevices(gatherDevId);
            if (null != deviceIotVOs && deviceIotVOs.size() <= 0) {
                logger.info("设备集合为空,mq设备缓存不更新刷新");
                return;
            }
            deviceIotVOs.forEach(item -> {
                redisService.setJson(MQ_DEVICE_KEY + item.getIotGatewayNum() + "-" + item.getGatherDevPointValue(), item);
            });
            logger.info("aop结束,mq设备缓存刷新完成");
        }
    }
}
